const Service = require('../../../core/service/ApiService');

class UserSvc extends Service{

    //家长登录
    async parentLogin(pInfo) {
        const dao = this.service.parent.user.dao;
        const encryptedPwd =this.helper.sha1(pInfo.pwd);
        if(await this.isPhone(pInfo.account)){
            const userInfo = await dao.findInfoByPhone(pInfo.account);
            if(!userInfo){
                throw new this.error.BusinessError('手机号有误');
            }

            if(userInfo.pwd!==encryptedPwd)
                throw new this.error.BusinessError('密码错误');
            const sName = await dao.findStudentNameByNumber(userInfo.child_number);
            this.ctx.session.parent_id = userInfo.parent_id;
            this.ctx.session.child_number = userInfo.child_number;
            console.log("1");
            return{
                parent_id:userInfo.parent_id,
                child_number:sName.number,
                student_name:sName.name
            }

        }
        else {
            const userInfo = await dao.findInfoByNumber(pInfo.account);
            if(!userInfo){
                throw new this.error.BusinessError('学号有误');
            }
            if(userInfo.pwd!==encryptedPwd)
                throw new this.error.BusinessError('密码错误');
            const sName = await dao.findStudentNameByNumber(userInfo.child_number);
            this.ctx.session.parent_id = userInfo.parent_id;
            this.ctx.session.child_number = userInfo.child_number;
            console.log("2");
            return{
                parent_id:userInfo.parent_id,
                child_number:sName.number,
                student_name:sName.name
            }
        }



    }
    async isPhone(num){
        const RegExp=/^[1][3,4,5,7,8][0-9]{9}$/;
        if (!RegExp.test(num)) {
            return false;
        } else {
            return true;
        }
    }


}


module.exports = UserSvc;
